{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "75417ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "72f50802",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>tpep_dropoff_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-07-01 00:51:04</td>\n",
       "      <td>2019-07-01 00:51:33</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-07-01 00:46:04</td>\n",
       "      <td>2019-07-01 01:05:46</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.16</td>\n",
       "      <td>20.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-01 00:25:09</td>\n",
       "      <td>2019-07-01 01:00:56</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.80</td>\n",
       "      <td>70.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-07-01 00:33:32</td>\n",
       "      <td>2019-07-01 01:15:27</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.46</td>\n",
       "      <td>66.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-07-01 00:00:55</td>\n",
       "      <td>2019-07-01 00:13:05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime tpep_dropoff_datetime  passenger_count  trip_distance  \\\n",
       "0  2019-07-01 00:51:04   2019-07-01 00:51:33              1.0           0.00   \n",
       "1  2019-07-01 00:46:04   2019-07-01 01:05:46              1.0           4.16   \n",
       "2  2019-07-01 00:25:09   2019-07-01 01:00:56              1.0          18.80   \n",
       "3  2019-07-01 00:33:32   2019-07-01 01:15:27              1.0          18.46   \n",
       "4  2019-07-01 00:00:55   2019-07-01 00:13:05              0.0           1.70   \n",
       "\n",
       "   total_amount  \n",
       "0          4.94  \n",
       "1         20.30  \n",
       "2         70.67  \n",
       "3         66.36  \n",
       "4         15.30  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load taxi data from January 2019 into a data frame, \n",
    "# using only the columns `tpep_pickup_datetime`, `tpep_dropoff_datetime`, \n",
    "# `passenger_count`, `trip_distance`, and `total_amount`, \n",
    "# making sure to load `tpep_pickup_datetime` and `tpep_dropoff_datetime` as `datetime` columns.\n",
    "\n",
    "filename = '../data/nyc_taxi_2019-07.csv'\n",
    "\n",
    "df = (\n",
    "    pd\n",
    "    .read_csv(filename,\n",
    "              usecols=['tpep_pickup_datetime',\n",
    "                       'tpep_dropoff_datetime',\n",
    "                       'trip_distance', \n",
    "                       'passenger_count',\n",
    "                       'total_amount'],\n",
    "              parse_dates=['tpep_pickup_datetime', \n",
    "                           'tpep_dropoff_datetime'])\n",
    ")\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fad76cdf-8294-4363-bdbb-5c97e06b9156",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime      datetime64[ns]\n",
       "tpep_dropoff_datetime     datetime64[ns]\n",
       "passenger_count                  float64\n",
       "trip_distance                    float64\n",
       "total_amount                     float64\n",
       "trip_time                timedelta64[ns]\n",
       "trip_time_group                 category\n",
       "dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d6cc8f8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   0 days 00:00:29\n",
       "1   0 days 00:19:42\n",
       "2   0 days 00:35:47\n",
       "3   0 days 00:41:55\n",
       "4   0 days 00:12:10\n",
       "Name: trip_time, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a new column, `trip_time`, containing the amount of time each taxi ride took.\n",
    "df['trip_time'] = (\n",
    "    df['tpep_dropoff_datetime'] - \n",
    "    df['tpep_pickup_datetime']\n",
    ")\n",
    "df['trip_time'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "edd1c762",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "70212"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What number of rides took less than 1 minute?\n",
    "df.loc[df['trip_time'] < '1 minute', 'trip_time'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "69955d6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.1126361022936828"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What percentage of rides took less than 1 minute?\n",
    "df.loc[df['trip_time'] < '1 minute', 'trip_time'].count() / df['trip_time'].count() * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "90fb378a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30.397584031219733"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the average fare paid by people taking these short trips?\n",
    "df.loc[df['trip_time'] < '1 minute', 'total_amount'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "49293127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16698"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What number of rides took more than 10 hours?\n",
    "df.loc[df['trip_time'] > '10 hours', 'trip_time'].count() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3a7186ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2646100045020782"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What percentage of rides took more than 10 hours?\n",
    "df.loc[df['trip_time'] > '10 hours', 'trip_time'].count() / df['trip_time'].count() * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bc223182",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  Now create a new column, `trip_time_group`, in which the values will be `short`\n",
    "# (< 10 minutes), `medium` (>= between 10 minutes and 1 hour), or `long` (> 1 hour).\n",
    "\n",
    "df['trip_time_group'] = (\n",
    "    pd.cut(\n",
    "           df['trip_time'],\n",
    "           bins=[pd.to_timedelta(arg)\n",
    "                 for arg in ['0 seconds', '10 minutes', \n",
    "                        '1 hour', '100 hours']],\n",
    "          labels=['short', 'medium', 'long'])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cfee9b27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "trip_time_group\n",
       "short     1.552411\n",
       "medium    1.585806\n",
       "long      1.700859\n",
       "Name: passenger_count, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What proportion of rides were in each group?\n",
    "df.groupby('trip_time_group', observed=False)['passenger_count'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "641707b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
